<cfparam name="resetFilters" default="false">
<cfset isFiltered = false />
<cfset queryParams = StructNew() />

<cfif NOT IsDefined("client.students.programYears") or resetFilters>
	<cfset client.students.programYears = request.currentProgramYear>
<cfelseif IsDefined("filterProgramYear")>
	<cfset client.students.programYears = filterProgramYear>
</cfif>
<cfif client.students.programYears neq request.currentProgramYear>
	<cfset isFiltered = true />
	<cfset queryParams.programYears = client.students.programYears />
	<!--- 
	override filterFollowUp and filterFollowUpStatus when filterProgramYear before 2012-2013 (or All if it is added as a criteria later)
	CAUTION: This block is order-of-operation critical.  This block must come before the filter blocks for filterFollowUp and filterFollowUpStatus
	 --->
	<cfif client.students.programYears eq "All" or client.students.programYears lt "2012-2013">
		<cfset filterFollowUp = "All" />
		<cfset filterFollowUpStatus = "All" />
	</cfif>
<cfelse>
	<cfset queryParams.programYears = request.currentProgramYear />
</cfif>

<cfif NOT IsDefined("client.students.followUp") or resetFilters>
	<cfset client.students.followUp = "All">
<cfelseif IsDefined("filterFollowUp")>
	<cfset client.students.followUp = filterFollowUp>
</cfif>
<cfif client.students.followUp neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.followUp = client.students.followUp />
</cfif>

<cfif NOT IsDefined("client.students.followUpStatus") or resetFilters>
	<cfset client.students.followUpStatus = "All">
<cfelseif IsDefined("filterFollowUpStatus")>
	<cfset client.students.followUpStatus = filterFollowUpStatus>
</cfif>
<cfif client.students.followUpStatus neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.Follow_Up_Status = client.students.followUpStatus />
</cfif>

<cfif NOT IsDefined("client.students.organizationIDs") or resetFilters>
	<cfset client.students.organizationIDs = "All">
<cfelseif IsDefined("filterOrganizationIDs")>
	<cfset client.students.organizationIDs = filterOrganizationIDs>
</cfif>
<cfif client.students.organizationIDs neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.organizationIDs = client.students.organizationIDs />
	<!--- <cfif IsUserInRole("Super User")>
		<cfset filterRegionIDs = "All" />
	</cfif> --->
</cfif>

<cfif IsUserInRole("Super User")>
	<cfif NOT IsDefined("client.students.regionIDs") or resetFilters>
		<cfset client.students.regionIDs = "1">
	<cfelseif IsDefined("filterRegionIDs")>
		<cfset client.students.regionIDs = filterRegionIDs>
	</cfif>
<cfelseif IsUserInRole("Regional Admin")>
	<cfset client.students.regionIDs = request.authUser.regionID>
</cfif>
<cfif client.students.regionIDs neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.regionIDs = client.students.regionIDs />
</cfif>

<cfwddx action="cfml2wddx" input="#queryParams#" output="client.students.queryParamsWDDX" />

<cfscript>
	studentGateway = new assets.calgrants.com.gateway.StudentGateway();
	students = studentGateway.query(argumentCollection = queryParams);
	organizationGateway = new assets.calgrants.com.gateway.OrganizationGateway();
	organizations = organizationGateway.query(regionIDs = IIf(IsUserInRole("Regional Admin"), request.authUser.regionID, ""), Organization_ID = IIf(IsUserInRole("Organizer") OR IsUserInRole("Site Contact"), request.authUser.organizationID, ""));
	storedProcedureGateway = new assets.calgrants.com.gateway.StoredProcedureGateway();
	regions = storedProcedureGateway.call("getDistinctRegions");
</cfscript>

<script type="text/javascript">
	var showImportModal = function() {
		$("#importModal").modal("show");
		resizeModal("importModal");
	}
	var hideImportModal = function() {
		$("#importModal").modal("hide");
		$("#importFile").val("");
		$("#selectedFile").text("");
	}
	var validate = function() {
		var importFileArray = $("#importFile").val().split(".");
		var fileExtension = importFileArray[importFileArray.length - 1];
		if (fileExtension == "xls" || fileExtension == "xlsx") {
			return true;
		} else if (fileExtension == "csv") {
			alert("Selected file is a CSV file, not Excel.\n\nPlease save the file as an Excel spreadsheet before importing.");
			return false;
		} else {
			alert("Selected file is not an Excel spreadsheet.\n\nOnly Excel spreadsheets can be imported.");
			return false;
		}
	};
	var openSelectFile = function() {
		$("#importFile").click();
	}
	$(function() {
		
		var fileInput = $("#importFile");
		
		fileInput.change(function() {
			$this = $(this);
			$("#selectedFile").text($this.val());
		})
	})
</script>

<cfoutput>

	<cfform name="importForm" id="importForm" action="#script_name#" method="post" enctype="multipart/form-data" onSubmit="return validate();">
		<cfinput type="hidden" name="action" value="import" />
		<cfinput type="hidden" name="collection" value="followUps" />
		<cfinput type="hidden" name="view" value="confirmImport" />
		<cfinput type="hidden" name="returnView" value="#view#" />
		<cfinput type="hidden" name="fileField" value="importFile" />
		
		<div id="importModal" class="modal modal-fullscreen" data-backdrop="static" style="display: none;">
			<div class="modal-header">
				<h3>Import Follow Up Requests</h3>
			</div>
			<div class="modal-body">
				<div class="container-fluid">
					<div class="row-fluid">
						<div class="span12">
							<h4>Instructions</h4>
							<p>You can import completed Follow Up Requests in an Excel spreadsheet.</p>
							<p>Please use a Follow Up Request export downloaded from this site as a template for the import file as there are certain fields that must be present in the import file.</p>
							<ol>
								<li>Export Follow Up Requests from this site using the Export Follow Up Request action on the Students list</li>
								<li>Contact each student and complete the follow up fields according to the table below</li>
								<li>Save the Follow Up Requests export as an Excel spreadsheet if it is a CSV file</li>
								<li>Return to this page and select the file using the Select a file button below and click Next</li>
								<li>Review the import and make any corrections as indicated by the import process</li>
								<li>Click the Import button to complete the import process</li>
							</ol>
							<h4>File</h4>
							<div class="well well-small">
								<p>
									<label class="required">Selected file</label>
									<div id="selectedFile">None</div>
								</p>
								<p><a href="##" class="btn btn-success" onClick="openSelectFile(); return false;">Select a file</a></p>
								<div style="height: 0; width: 0; overflow: hidden">
									<cfinput type="file" name="importFile" required="yes" message="Follow Up Requests File is required" accept="application/msexcel">
								</div>
							</div>
							<h4>Field Descriptions</h4>
							<p>The following fields (columns in Excel) must be in the import file and are the only fields that will be updated in the student record.  All other fields will be ignored.</p>
							<table class="table table-striped table-bordered" style="font-size: .8em;">
								<thead>
									<tr>
										<th>Column</th>
										<th>Acceptable Values</th>
										<th>Description</th>
									</tr>
								</thead>
								<tbody>
									<tr>
										<td>STUDENT_ID</td>
										<td>System generated Student ID</td>
										<td>This value should not be changed from the value downloaded in the Follow Up Request export</td>
									</tr>
									<tr>
										<td>FOLLOW_UP_STATUS</td>
										<td>Not Complete, Complete</td>
										<td>Not Complete until follow up is finished, then Complete</td>
									</tr>
									<tr>
										<td>FOLLOW_UP_FAFSA</td>
										<td>Yes, No, "blank"</td>
										<td>"Blank" until student is contacted, then Yes if FAFSA assistance was provided, otherwise No</td>
									</tr>
									<tr>
										<td>FOLLOW_UP_CA_DREAM_ACT</td>
										<td>Yes, No, "blank"</td>
										<td>"Blank" until student is contacted, then Yes if CA Dream Act assistance was provided, otherwise No</td>
									</tr>
									<tr>
										<td>FOLLOW_UP_TAXES</td>
										<td>Yes, No, "blank"</td>
										<td>"Blank" until student is contacted, then Yes if tax assistance was provided, otherwise No</td>
									</tr>
									<tr>
										<td>FOLLOW_UP_INCOME_ASSETS</td>
										<td>Yes, No, "blank"</td>
										<td>"Blank" until student is contacted, then Yes if income/assets assistance was provided, otherwise No</td>
									</tr>
									<tr>
										<td>FOLLOW_UP_CAL_GRANT_DEADLINE</td>
										<td>Yes, No, "blank"</td>
										<td>"Blank" until student is contacted, then Yes if Cal Grant deadline assistance was provided, otherwise No</td>
									</tr>
									<tr>
										<td>FOLLOW_UP_CAL_GRANT_REQUIREMENTS</td>
										<td>Yes, No, "blank"</td>
										<td>"Blank" until student is contacted, then Yes if Cal Grant requirement (including GPA) assistance was provided, otherwise No</td>
									</tr>
									<tr>
										<td>FOLLOW_UP_OTHER</td>
										<td>Any value up to 200 characters, "blank"</td>
										<td>"Blank" until student is contacted, then descriptive text of assistance given that is not listed above.  For example, "Chafee assistance".</td>
									</tr>
									<tr>
										<td>FOLLOW_UP_NOTES</td>
										<td>Any value up to 2000 characters, "blank"</td>
										<td>"Blank" until student is contacted, then any notes associated with the follow up request.</td>
									</tr>
								</tbody>
							</table>
						</div>
					</div>
				</div>
			</div>
			<div class="modal-footer">
				<a href="##" class="btn btn-success" onClick="hideImportModal(); return false;">Cancel</a>
				<button type="submit" name="importButton" class="btn btn-success">Next</button>
			</div>
		</div>
	</cfform>

	<h3>Students<cfif isFiltered> <small><em>filtered</em></small></cfif></h3>
	 
	<div class="row-fluid" style="margin-bottom: 10px;">
		<div class="span12">
			<a id="filterLink" href="##" class="btn" onclick="toggleFilter('filter')">Show filters</a>
			<div class="btn-group">
				<button class="btn dropdown-toggle" data-toggle="dropdown">Actions</button>
				<button class="btn dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></button>
				<ul class="dropdown-menu">
					<cfif students.RecordCount gt 0>
						<li><a href="?action=export&collection=students" target="_new">Export</a></li>
					</cfif>
					<cfif IsUserInRole("Super User")>
						<li><a href="?action=export&collection=allStudents" target="_new">Export - All</a></li>
					</cfif>
					<cfif students.RecordCount gt 0 and client.students.programYears neq "All" and client.students.programYears gte "2012-2013" and client.students.followUp neq "No">
						<li><a href="?action=export&collection=followUps" target="_new">Export - Follow Up Format</a></li>
					</cfif>
					<cfif IsUserInRole("Super User")>
						<li><a href="?action=export&collection=allFollowUps" target="_new">Export - All, Follow Up Format</a></li>
					</cfif>
					<li><a href="##" onClick="showImportModal(); return false;">Import - Follow Up Format</a></li>
				</ul>
			</div>
			<span class="pull-right"><strong>Count:</strong> #students.RecordCount#</span>
		</div>
	</div>

	<cfform name="studentsForm" id="studentsForm" action="#script_name#" method="post">
		<cfinput type="hidden" name="view" value="#view#">
		
		<div id="filter" class="alert alert-info alert-block" style="display: none;">
			<h4>Filters</h4>
			<table cellpadding="6">
				<tr valign="top">
					<td>
						<label>Program Year</label>
						<cfselect name="filterProgramYear" size="1" multiple="false" required="yes" message="Program Year filter is required" style="width: 150px;">
							<cfloop index="y" from="2009" to="#ListFirst(application.currentProgramYear, '-')#">
								<cfset py = "#y#-" & y+1>
								<option value="#py#"<cfif client.students.programYears eq py> selected</cfif>>#py#</option>
							</cfloop>
						</cfselect>
					</td>
					<cfif IsUserInRole("Super User")>
						<td>
							<label>Region</label>
							<cfselect name="filterRegionIDs" size="1" multiple="false" required="yes" message="Region filter is required" style="width: 150px;">
								<cfloop query="regions">
									<option value="#Region_ID#"<cfif ListFind(client.students.regionIDs, Region_ID)> selected</cfif>>#Region_ID#</option>
								</cfloop>
							</cfselect>
						</td>
					</cfif>
					<td>
						<label>Organizations</label>
						<cfselect name="filterOrganizationIDs" size="7" multiple="true" required="yes" message="Organization filter is required" style="width: auto;">
							<option value="All"<cfif client.students.organizationIDs eq "All"> selected</cfif>>All</option>
							<cfloop query="organizations">
								<option value="#Organization_ID#"<cfif ListFind(client.students.organizationIDs, Organization_ID)> selected</cfif>>#Organization# - #City#</option>
							</cfloop>
						</cfselect>
					</td>
					<td>
						<label>Follow Up</label>
						<cfif client.students.programYears neq "All" and client.students.programYears gte "2012-2013">
							<cfselect name="filterFollowUp" size="1" multiple="false" required="yes" message="Follow Up filter is required" style="width: 150px;">
								<option value="All"<cfif client.students.followUp eq "All"> selected</cfif>>All</option>
								<option value="Yes"<cfif client.students.followUp eq "Yes"> selected</cfif>>Yes</option>
								<option value="No"<cfif client.students.followUp eq "No"> selected</cfif>>No</option>
							</cfselect>
						<cfelse>
							<span class="muted"><small>Not available as filtered</small></span>
						</cfif>
					</td>
					<td>
						<label>Follow Up Status</label>
						<cfif client.students.programYears neq "All" and client.students.programYears gte "2012-2013">
							<cfselect name="filterFollowUpStatus" size="1" multiple="false" required="yes" message="Follow Up Status filter is required" style="width: 150px;">
								<option value="All"<cfif client.students.followUpStatus eq "All"> selected</cfif>>All</option>
								<option value="Complete"<cfif client.students.followUpStatus eq "Complete"> selected</cfif>>Complete</option>
								<option value="Not Complete"<cfif client.students.followUpStatus eq "Not Complete"> selected</cfif>>Not Complete</option>
							</cfselect>
						<cfelse>
							<span class="muted"><small>Not available as filtered</small></span>
						</cfif>
					</td>
				</tr>
				<tr>
					<td colspan="5">
						<button type="submit" class="btn btn-info">Apply</button> <a href="#script_name#?view=students&resetFilters=true" class="btn btn-info">Clear</a>
					</td>
				</tr>
			</table>
		</div>

		<cfif students.RecordCount gt 0>

			<table id="studentsList" style="width: 100%">
				<thead>
					<tr>
						<th>ID</th>
						<th>Program Year</th>
						<th>Organization</th>
						<cfif IsUserInRole("Super User")>
							<th>Region</th>
						</cfif>
						<th>Site</th>
						<th>Date/Time</th>
						<th>Name</th>
						<cfif client.students.programYears neq "All" and client.students.programYears gte "2012-2013">
							<th>Follow Up?</th>
							<th>Follow Up Status</th>
							<th>GPA</th>
							<th>FAFSA/Dream Act</th>
							<th>Dreamer</th>
						</cfif>
					</tr>
				</thead>
				<tbody>
					<cfloop query="students">
						<tr>
							<td>
							<cfif IsUserInRole("Super User") or IsUserInRole("Regional Admin")>
								<a href="#script_name#?view=editStudent&Student_ID=#Student_ID#">#Student_ID#</a>
							<cfelse>
								#Student_ID#
							</cfif>
							</td>
							<td>#Program_Year#</td>
							<td>
								<!--- <cfif Organization_Deleted>
									<font color="red">#Organization#</font>
								<cfelseif Organization_Archived>
									<font color="silver">#Organization#</font>
								<cfelseif NOT Organization_Approved>
									<font color="orange">#Organization#</font>
								<cfelseif Organization_Approved> --->
									#Organization#
								<!--- </cfif> --->
							</td>
							<cfif IsUserInRole("Super User")>
								<td>#Region_ID#</td>
							</cfif>
							<td>#Site#</td>
							<td><span class="sortHide">#DateFormat(Workshop_Date, 'yyyymmdd')##TimeFormat(Start_Time, 'HHmm')#</span>#DateFormat(Workshop_Date, "m/d/yyyy")# #TimeFormat(Start_Time, "h:mmtt")#&nbsp;-&nbsp;#TimeFormat(End_Time, "h:mmtt")#</td>
							<td>
								<cfif Len(Trim(First_Name)) gt 0 OR Len(Trim(Last_Name)) gt 0>
									#First_Name# #Last_Name#
								<cfelse>
									#GDS_First_Name# #GDS_Last_Name#
								</cfif>
							</td>
							<cfif client.students.programYears neq "All" and client.students.programYears gte "2012-2013">
								<td>
									<cfif Program_Year gte "2012-2013">
										<cfif Q15 eq 1>
											No
										<cfelseif Q15 eq 2 or Q15 eq 3>
											Yes
										<cfelse>
											No
										</cfif>
									<cfelse>
										<span class="muted">N/A</span>
									</cfif>
								</td>
								<td>
									<cfif Q15 eq 2 or Q15 eq 3>
										<cfif Follow_Up_Status eq "Complete">
											Complete
										<cfelse>
											Not Complete
										</cfif>
									<cfelse>
										<span class="muted">N/A</span>
									</cfif>
								</td>
								<td>
									<cfif Len(GPA) gt 0>
										Yes
									<!--- <cfelseif On_Time_GPA eq "N">
										No
									<cfelse>
										<span class="muted">No data</span> --->
									</cfif>
								</td>
								<td>
									<cfif On_Time_ISIR eq 1 or On_Time_Dream_Act eq 1>
										Yes
									<cfelseif On_Time_ISIR eq 0 AND On_Time_Dream_Act eq 0>
										No
									<cfelse>
										<span class="muted">No data</span>
									</cfif>
								</td>
								<td>
									<cfif On_Time_Dream_Act eq 1>
										Yes
									<cfelseif (SSN_Code eq 1 AND Len(Trim(SSN)) gt 9) OR Len(GDS_SSN) eq 9 OR On_Time_ISIR eq 1>
										No
									<cfelseif SSN_Code eq "2" or ListFind(q16, "5") gt 0>
										Maybe
									<cfelse>
										Unknown
									</cfif>
								</td>
							</cfif>
						</tr>
				</cfloop>
				</tbody>
			</table>
			
			<script type="text/javascript">
				$(function() {
					$("##studentsList").kendoGrid({
						sortable: {
							mode: "single",
							allowUnsort: false
						},				
						scrollable: true	
					})
					$("##studentsList").show();
				})
			</script>
			
		<cfelse>
		
			<div class="alert alert-info notFound">No students</div>
	
			<script type="text/javascript" language="javascript">
			<!--
				$(function() {
					$("##filter").show();
				});
			// -->
			</script>
		
		</cfif>

	</cfform>

</cfoutput>